<?php 
namespace app\api\controller;

use think\Exception;
use think\Controller;
use app\common\exception\BaseException;
use app\api\model\Ip as IpModel;

class Ip extends Controller {
    public function addIp() {
        $ip = $this->request->ip();
        $id = $this->request->param('id');
        $obj = new IpModel();
        $count = $obj->where("article_id", $id)->find();
        if($count) {
            $count = $count->count();
        } else {
            $count = 0;
        }
        $data = $obj->where("article_id", $id)->where("ip", $ip)->find();
        if($data) {
            return json([
                "msg"=> 'success',
                "code"=>20000,
                "count"=> $count 
            ],200);
        }else {
            $obj->ip = $ip;
            $obj->article_id = $id;
            $res = $obj->save();
            if($res) {
                return json([
                    "msg"=> 'success',
                    "code"=>20000,
                    "count"=> $count+1 
                ],200); 
            }
        }
        // dump($ip);
    }
}